package com.katze.boot.hadoop.controller;

import com.katze.boot.hadoop.entity.HadoopEntity;
import com.katze.boot.hadoop.service.FeignHadoopService;
import com.katze.boot.plugins.security.annotations.RestControllerMapping;
import com.katze.boot.plugins.security.annotations.RestRequestMapping;
import com.katze.common.Message;
import com.katze.common.controller.WebClientController;
import com.katze.common.env.AppEnvironment;
import com.katze.common.http.HttpUtils;
import com.katze.common.http.ResponseDownloadHelper;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

@RestControllerMapping("/hadoop")
public class HadoopController extends WebClientController<FeignHadoopService> {
    private static final Logger log = LoggerFactory.getLogger(HadoopController.class);

    @RestRequestMapping(value = "/list/{uid}", skip = true)
    public Message list (@PathVariable("uid") String uid, HadoopEntity entity) {
        return execute(uid).list(entity.getPath(), entity.getStatus());
    }

    @RestRequestMapping(value = "/{method}/{uid}", method = RequestMethod.POST, description = "mkdirs:新建文件夹; rename:重命名; move:移动; copy:复制; ")
    public Message post (@PathVariable("uid") String uid, @PathVariable("method") String method, HadoopEntity entity) {
       return execute(uid).post(method, entity);
    }

    @RestRequestMapping(value = "/delete/{uid}", method = RequestMethod.DELETE, description = "delete:删除;")
    public Message delete (@PathVariable("uid") String uid, String path) {
        return execute(uid).delete(path);
    }

    @RestRequestMapping(value = "/upload/{uid}", method = RequestMethod.POST, description = "上传文件")
    public Message upload (@PathVariable("uid") String uid, MultipartFile file, String target) {
        return execute(uid).upload(file, target);
    }

    @RestRequestMapping(value = "/download/{uid}", method = RequestMethod.POST, description = "下载文件")
    public void download (HttpServletResponse response, @PathVariable("uid") String uid, HadoopEntity entity) {
        long start = System.currentTimeMillis(), end;
        ResponseEntity<Resource> templateResponse = execute(uid).download(entity.getPath());
        end = System.currentTimeMillis();
        String filename = templateResponse.getHeaders().get(ResponseDownloadHelper.CONTENT_FILENAME).getFirst();
        filename = URLDecoder.decode(filename, StandardCharsets.UTF_8);
        Path file = null;
        try (InputStream in = templateResponse.getBody().getInputStream()){
            file = Files.write(AppEnvironment.TEMP_DIR.getPath(filename), IOUtils.toByteArray(in));
        } catch (Exception e) {
            log.error("文件写入本地临时文件失败", e);
        } finally {
            log.info("打包耗时:{}ms, 写入磁盘耗时:{}ms", end - start, System.currentTimeMillis() - end);
        }
        if (templateResponse.getStatusCode().value() == 200 && file != null) {
            HttpUtils.download(response).load(file.toFile());
        }
    }

}
